package com.ruoyi.web.controller.sts;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.sts.domain.*;
import com.ruoyi.system.domain.SysHoliday;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysHolidayService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sts.service.IStsDataDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客流数据Controller
 * 
 * @author maoxiaodong
 * @date 2025-04-20
 */
@Controller
@RequestMapping("/sts/data/detail")
public class StsDataDetailController extends BaseController
{
    private String prefix = "sts/data";

    @Autowired
    private IStsDataDetailService stsDataDetailService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysHolidayService sysHolidayService;

    //@RequiresPermissions("sts:detail:view")
    @GetMapping()
    public String detail(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts2 = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts2", depts2);
        params.put("level", 2);
        sysDept.setParams(params);
        List<SysDept> depts3 = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts3", depts3);
        mmap.put("startTime",DateUtils.getDate());
        mmap.put("endTime", DateUtils.getDate());
        return prefix + "/detail";
    }

    //@RequiresPermissions("sts:detail:statistic")
    @GetMapping("/statistic/dept2day")
    public String dept2day(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);
        mmap.put("day", DateUtils.getDate());
        return  "sts/statistic/dept2day";
    }

    @GetMapping("/statistic/dept2week")
    public String dept2week(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);

        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY); // 设置周首日为周一
        cal.setTime(new java.util.Date());

        // 获取本周一
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

        // 获取本周日
        cal.add(Calendar.DAY_OF_WEEK, 6);
        String sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

        mmap.put("nowDate", DateUtils.getDate());
        mmap.put("startTime",monday);
        mmap.put("endTime", sunday);
        return  "sts/statistic/dept2week";
    }

    @GetMapping("/statistic/dept2month")
    public String dept2month(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);
        mmap.put("month", DateUtils.dateTimeNow("yyyy-MM"));
        return  "sts/statistic/dept2month";
    }

    @GetMapping("/statistic/dept2year")
    public String dept2year(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);
        mmap.put("year", DateUtils.dateTimeNow("yyyy"));
        return  "sts/statistic/dept2year";
    }

    @GetMapping("/statistic/dept2custom")
    public String dept2custom(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);

        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY); // 设置周首日为周一
        cal.setTime(new java.util.Date());

        // 获取本周一
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        mmap.put("startTime",monday);
        mmap.put("endTime", DateUtils.getDate());
        return  "sts/statistic/dept2custom";
    }


    @GetMapping("/statistic/dept3day")
    public String dept3day(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 2);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);
        mmap.put("day", DateUtils.getDate());
        return  "sts/statistic/dept3day";
    }

    @GetMapping("/statistic/dept3week")
    public String dept3week(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 2);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);

        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY); // 设置周首日为周一
        cal.setTime(new java.util.Date());

        // 获取本周一
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

        // 获取本周日
        cal.add(Calendar.DAY_OF_WEEK, 6);
        String sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

        mmap.put("nowDate", DateUtils.getDate());
        mmap.put("startTime",monday);
        mmap.put("endTime", sunday);
        return  "sts/statistic/dept3week";
    }

    @GetMapping("/statistic/dept3month")
    public String dept3month(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 2);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);
        mmap.put("month", DateUtils.dateTimeNow("yyyy-MM"));
        return  "sts/statistic/dept3month";
    }

    @GetMapping("/statistic/dept3year")
    public String dept3year(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 2);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);
        mmap.put("year", DateUtils.dateTimeNow("yyyy"));
        return  "sts/statistic/dept3year";
    }

    @GetMapping("/statistic/dept3custom")
    public String dept3custom(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 2);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);

        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY); // 设置周首日为周一
        cal.setTime(new java.util.Date());

        // 获取本周一
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        mmap.put("startTime",monday);
        mmap.put("endTime", DateUtils.getDate());
        return  "sts/statistic/dept3custom";
    }

    @GetMapping("/statistic/dept2comp")
    public String dept2comp(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);

        mmap.put("startTime",DateUtils.getDate());
        mmap.put("endTime", DateUtils.getDate());
        return  "sts/statistic/dept2comp";
    }

    @GetMapping("/statistic/dept3comp")
    public String dept3comp(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 2);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);

        mmap.put("startTime",DateUtils.getDate());
        mmap.put("endTime", DateUtils.getDate());
        return  "sts/statistic/dept3comp";
    }

    @GetMapping("/statistic/dept2dateComp")
    public String dept2dateComp(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);

        mmap.put("startTime",DateUtils.getDate());
        mmap.put("endTime", DateUtils.getDate());
        return  "sts/statistic/dept2dateComp";
    }

    @GetMapping("/statistic/dept3dateComp")
    public String dept3dateComp(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 2);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);

        mmap.put("startTime",DateUtils.getDate());
        mmap.put("endTime", DateUtils.getDate());
        return  "sts/statistic/dept3dateComp";
    }


    @GetMapping("/statistic/dept2peak")
    public String dept2peak(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts", depts);
        mmap.put("startTime",DateUtils.getDate());
        mmap.put("endTime", DateUtils.getDate());
        return  "sts/statistic/dept2peak";
    }


    @GetMapping("/statistic/dept2holiday")
    public String dept2holiday(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts = sysDeptService.selectDeptListByLevel(sysDept);
        String year = DateUtils.dateTimeNow("yyyy");
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        Long deptId1 = -1L;
        SysDept sysDept1 = sysDeptService.selectDeptById(deptId);
        if(sysDept1 != null){
            String [] ancestors =  sysDept1.getAncestors().split(",");
            if(ancestors.length > 1){
                deptId1 = Long.valueOf(ancestors[1]);
            }else{
                deptId1 = deptId;
            }

        }
        List<SysHoliday> holidays = sysHolidayService.selectSysHolidayListByYear(deptId1, year);
        mmap.put("holidays", holidays);
        mmap.put("depts", depts);
        mmap.put("year", DateUtils.dateTimeNow("yyyy"));
        return  "sts/statistic/dept2holiday";
    }

    @GetMapping("/statistic/visual")
    public String visual()
    {
        return  "sts/statistic/visual";
    }





    /**
     * 查询客流数据列表
     */
    @RequiresPermissions("sts:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StsDataDetail stsDataDetail)
    {
        startPage();
        List<StsDataDetail> list = stsDataDetailService.selectStsDataDetailList(stsDataDetail);
        return getDataTable(list);
    }

    /**
     * 导出客流数据列表
     */
    @RequiresPermissions("sts:detail:export")
    @Log(title = "客流数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StsDataDetail stsDataDetail)
    {
        List<StsDataDetail> list = stsDataDetailService.selectStsDataDetailList(stsDataDetail);
        ExcelUtil<StsDataDetail> util = new ExcelUtil<StsDataDetail>(StsDataDetail.class);
        return util.exportExcel(list, "客流数据数据");
    }

    /**
     * 新增客流数据
     */
    @RequiresPermissions("sts:detail:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客流数据
     */
    @RequiresPermissions("sts:detail:add")
    @Log(title = "客流数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StsDataDetail stsDataDetail)
    {
        return toAjax(stsDataDetailService.insertStsDataDetail(stsDataDetail));
    }

    /**
     * 修改客流数据
     */
    //@RequiresPermissions("sts:detail:edit")
    @GetMapping("/edit")
    public String edit(ModelMap mmap)
    {
        SysDept sysDept = new SysDept();
        Map<String, Object> params = new HashMap<>();
        params.put("level", 1);
        sysDept.setParams(params);
        List<SysDept> depts2 = sysDeptService.selectDeptListByLevel(sysDept);
        mmap.put("depts2", depts2);
        params.put("level", 2);
        sysDept.setParams(params);
        if(!CollectionUtils.isEmpty(depts2)){
            List<SysDept> depts3 = sysDeptService.selectByParentId(depts2.get(0).getDeptId());
            mmap.put("depts3", depts3);
        }
        mmap.put("startTimeEdit",DateUtils.dateTimeNow("yyyy-MM-dd HH:mm"));
        mmap.put("startTime",DateUtils.getDate());
        mmap.put("endTime", DateUtils.getDate());
        return prefix + "/edit";
    }

    /**
     * 修改保存客流数据
     */
    @RequiresPermissions("sts:detail:edit")
    @Log(title = "客流数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StsDataDetail stsDataDetail)
    {
        return toAjax(stsDataDetailService.updateStsDataDetail(stsDataDetail));
    }

    /**
     * 修改保存客流数据
     */
    //@RequiresPermissions("sts:detail:edit")
    @Log(title = "客流数据修改数量", businessType = BusinessType.UPDATE)
    @PostMapping("/editNum")
    @ResponseBody
    public AjaxResult editNum(StsDataDetail stsDataDetail)
    {
        return toAjax(stsDataDetailService.updateStsDataDetailNum(stsDataDetail));
    }

    /**
     * 删除客流数据
     */
    @RequiresPermissions("sts:detail:remove")
    @Log(title = "客流数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stsDataDetailService.deleteStsDataDetailByIds(ids));
    }

    /**
     * 统计客流数据列表
     */
    //@RequiresPermissions("sts:detail:list")
    @PostMapping("/statistic")
    @ResponseBody
    public List<StsDataDetail> selectStsDataStatisticList(StsDataStatisticQuery stsDataStatisticQuery){
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        if(sysDept != null){
            String [] ancestors =  sysDept.getAncestors().split(",");
            if(ancestors.length > 1){
                stsDataStatisticQuery.setDeptId1(Long.valueOf(ancestors[1]));
            }else{
                stsDataStatisticQuery.setDeptId1(deptId);

            }

        }
        return stsDataDetailService.selectStsDataStatisticList(stsDataStatisticQuery);
    }

    /**
     * 统计客流数据列表
     */
    //@RequiresPermissions("sts:detail:list")
    @PostMapping("/deptStatistic")
    @ResponseBody
    public List<StsDataDetail> selectStsDataStatisticList(StsDataDeptStatisticQuery stsDataDeptStatisticQuery){
        if(CollectionUtils.isEmpty(stsDataDeptStatisticQuery.getDeptIds())){
            List<Long> deptIds = new ArrayList<>();
            deptIds.add(-1L);
            stsDataDeptStatisticQuery.setDeptIds(deptIds);
        }
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        if(sysDept != null){
            String [] ancestors =  sysDept.getAncestors().split(",");
            if(ancestors.length > 1){
                stsDataDeptStatisticQuery.setDeptId1(Long.valueOf(ancestors[1]));
            }else{
                stsDataDeptStatisticQuery.setDeptId1(deptId);

            }

        }
        return stsDataDetailService.selectStsDataDeptStatisticList(stsDataDeptStatisticQuery);
    }

    @PostMapping("/deptHolidayStatistic")
    @ResponseBody
    public List<StsDataDetailHoliday> deptHolidayStatistic(StsDataDeptStatisticQuery stsDataDeptStatisticQuery){
        if(CollectionUtils.isEmpty(stsDataDeptStatisticQuery.getDeptIds())){
            List<Long> deptIds = new ArrayList<>();
            deptIds.add(-1L);
            stsDataDeptStatisticQuery.setDeptIds(deptIds);
        }
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        Long deptId1 = -1L;
        if(sysDept != null){
            String [] ancestors =  sysDept.getAncestors().split(",");
            if(ancestors.length > 1){
                deptId1 = Long.valueOf(ancestors[1]);
                stsDataDeptStatisticQuery.setDeptId1(deptId1);
            }else{
                deptId1 = deptId;
                stsDataDeptStatisticQuery.setDeptId1(deptId);

            }

        }

        List<StsDataDetailHoliday> stsDataDetailHolidays = new ArrayList<>();
        List<SysHoliday> sysHolidays;
        if(CollectionUtils.isEmpty(stsDataDeptStatisticQuery.getHolidayIds())){
            sysHolidays = sysHolidayService.selectSysHolidayListByYear(deptId1, stsDataDeptStatisticQuery.getYear());
        }else{
            sysHolidays = sysHolidayService.selectSysHolidayListByIds(stsDataDeptStatisticQuery.getHolidayIds());
        }
        for(SysHoliday sysHoliday:sysHolidays){
            stsDataDeptStatisticQuery.setStartTime(sysHoliday.getStartDate());
            stsDataDeptStatisticQuery.setEndTime(sysHoliday.getEndTime());
            List<StsDataDetail> stsDataDetails = stsDataDetailService.selectStsDataDeptStatisticList(stsDataDeptStatisticQuery);
            if(!CollectionUtils.isEmpty(stsDataDetails)){
                for(StsDataDetail stsDataDetail:stsDataDetails){
                    StsDataDetailHoliday stsDataDetailHoliday = new StsDataDetailHoliday();
                    BeanUtils.copyBeanProp(stsDataDetailHoliday, stsDataDetail);
                    Integer day = DateUtils.differentDaysByMillisecond(sysHoliday.getStartDate(), sysHoliday.getEndTime()) + 1;
                    stsDataDetailHoliday.setHolidayDay(day);
                    stsDataDetailHoliday.setHolidayName(sysHoliday.getName());
                    stsDataDetailHoliday.setHolidayStartDate(sysHoliday.getStartDate());
                    stsDataDetailHoliday.setHolidayEndTime(sysHoliday.getEndTime());
                    stsDataDetailHolidays.add(stsDataDetailHoliday);
                }

            }
        }
        return stsDataDetailHolidays;
    }


    @PostMapping("/day2data")
    @ResponseBody
    public Map<String, StsDataDetail> day2data(Long deptId2){
        Map<String, StsDataDetail> data = new HashMap<String, StsDataDetail>(){{

            put("todayData", null);
            put("yesterdayData", null);
        }};
        StsDataStatisticQuery todayStsDataStatisticQuery = new StsDataStatisticQuery();
        todayStsDataStatisticQuery.setDay(DateUtils.getDate());
        todayStsDataStatisticQuery.setDateFormat("%Y-%m-%d");
        todayStsDataStatisticQuery.setDeptId2(deptId2);
        StsDataStatisticQuery yesterdayStsDataStatisticQuery = new StsDataStatisticQuery();
        Date yesterday = DateUtils.addDays(DateUtils.getNowDate(), -1);
        yesterdayStsDataStatisticQuery.setDay(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, yesterday ));
        yesterdayStsDataStatisticQuery.setDateFormat("%Y-%m-%d");
        yesterdayStsDataStatisticQuery.setDeptId2(deptId2);


        List<StsDataDetail> todayStsDataDetails = stsDataDetailService.selectStsDataStatisticList(todayStsDataStatisticQuery);
        if(!CollectionUtils.isEmpty(todayStsDataDetails)){
            data.put("todayData", todayStsDataDetails.get(0));
        }
        List<StsDataDetail> yesterdayStsDataDetails = stsDataDetailService.selectStsDataStatisticList(yesterdayStsDataStatisticQuery);
        if(!CollectionUtils.isEmpty(yesterdayStsDataDetails)){
            data.put("yesterdayData", yesterdayStsDataDetails.get(0));
        }
        return data;
    }

    /**
     * 统计客流数据列表
     */
    //@RequiresPermissions("sts:detail:list")
    @PostMapping("/statisticTop10")
    @ResponseBody
    public List<StsDataDetail> statisticTop10(StsDataDeptStatisticQuery stsDataDeptStatisticQuery){
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        if(sysDept != null){
            String [] ancestors =  sysDept.getAncestors().split(",");
            if(ancestors.length > 1){
                stsDataDeptStatisticQuery.setDeptId1(Long.valueOf(ancestors[1]));
            }else{
                stsDataDeptStatisticQuery.setDeptId1(deptId);

            }

        }
        return stsDataDetailService.selectStsDataDeptStatisticTop10List(stsDataDeptStatisticQuery);
    }


    @PostMapping("/statData")
    @ResponseBody
    public Map<String, StsDataDetail> statData(Long deptId2, String type, String dateStr){
        Map<String, StsDataDetail> data = new HashMap<String, StsDataDetail>(){{
            put("todayData", null);
            put("yesterdayData", null);
        }};
        if (!"week".equals(type)) {
            String day = StringUtils.isEmpty(dateStr) ? DateUtils.getDate() : dateStr;
            StsDataStatisticQuery todayStsDataStatisticQuery = new StsDataStatisticQuery();
            if ("day".equals(type)) {
                todayStsDataStatisticQuery.setDay(day);
                todayStsDataStatisticQuery.setDateFormat("%Y-%m-%d");
            } else if ("month".equals(type)) {
                todayStsDataStatisticQuery.setMonth(dateStr);
                todayStsDataStatisticQuery.setDateFormat("%Y-%m");
            } else if ("year".equals(type)) {
                todayStsDataStatisticQuery.setYear(dateStr);
                todayStsDataStatisticQuery.setDateFormat("%Y");
            }
            todayStsDataStatisticQuery.setDeptId2(deptId2);
            StsDataStatisticQuery yesterdayStsDataStatisticQuery = new StsDataStatisticQuery();
            if ("day".equals(type)) {
                Date yesterday = DateUtils.addDays(DateUtils.parseDate(day), -1);
                yesterdayStsDataStatisticQuery.setDay(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, yesterday));
                yesterdayStsDataStatisticQuery.setDateFormat("%Y-%m-%d");
            } else if ("month".equals(type)) {
                String month = DateUtils.parseDateToStr(DateUtils.YYYY_MM, DateUtils.addMonths(DateUtils.parseDate(dateStr), -1));
                yesterdayStsDataStatisticQuery.setMonth(month);
                yesterdayStsDataStatisticQuery.setDateFormat("%Y-%m");
            } else if ("year".equals(type)) {
                String month = String.valueOf(Long.parseLong(dateStr) - 1);
                yesterdayStsDataStatisticQuery.setYear(month);
                yesterdayStsDataStatisticQuery.setDateFormat("%Y");
            }
            yesterdayStsDataStatisticQuery.setDeptId2(deptId2);


            List<StsDataDetail> todayStsDataDetails = stsDataDetailService.selectStsDataStatisticList(todayStsDataStatisticQuery);
            if (!CollectionUtils.isEmpty(todayStsDataDetails)) {
                data.put("todayData", todayStsDataDetails.get(0));
            }
            List<StsDataDetail> yesterdayStsDataDetails = stsDataDetailService.selectStsDataStatisticList(yesterdayStsDataStatisticQuery);
            if (!CollectionUtils.isEmpty(yesterdayStsDataDetails)) {
                data.put("yesterdayData", yesterdayStsDataDetails.get(0));
            }
        } else {
            String day = StringUtils.isEmpty(dateStr) ? DateUtils.getDate() : dateStr;
            StsDataStatisticQuery stsDataStatisticQuery1 = new StsDataStatisticQuery();
            String[] dateSplit = day.split("-");
            // 假设我们要找的日期是2023-04-15
            LocalDate date = LocalDate.of(Integer.parseInt(dateSplit[0]), Integer.parseInt(dateSplit[1]), Integer.parseInt(dateSplit[2]));
            // 获取该日期所在周的周一
            LocalDate monday1 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 获取该日期所在周的周日
            LocalDate sunday1 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            stsDataStatisticQuery1.setDeptId2(deptId2);
            stsDataStatisticQuery1.setStartTime(Date.from(monday1.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            stsDataStatisticQuery1.setEndTime(Date.from(sunday1.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            List<StsDataDetail> stsDataDetails1 = stsDataDetailService.selectStsWeekDataStatisticList(stsDataStatisticQuery1);
            if (stsDataDetails1 != null && !stsDataDetails1.isEmpty()) {
                data.put("todayData", stsDataDetails1.get(0));
            }

            StsDataStatisticQuery stsDataStatisticQuery2 = new StsDataStatisticQuery();
            String date2 = DateUtils.dateTime(DateUtils.addDays(DateUtils.parseDate(day), -7));
            String[] dateSplit2 = date2.split("-");
            // 假设我们要找的日期是2023-04-15
            LocalDate localDate2 = LocalDate.of(Integer.parseInt(dateSplit2[0]), Integer.parseInt(dateSplit2[1]), Integer.parseInt(dateSplit2[2]));
            // 获取该日期所在周的周一
            LocalDate monday2 = localDate2.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 获取该日期所在周的周日
            LocalDate sunday2 = localDate2.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            stsDataStatisticQuery2.setDeptId2(deptId2);
            stsDataStatisticQuery2.setStartTime(Date.from(monday2.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            stsDataStatisticQuery2.setEndTime(Date.from(sunday2.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            List<StsDataDetail> stsDataDetails2 = stsDataDetailService.selectStsWeekDataStatisticList(stsDataStatisticQuery2);
            if (stsDataDetails2 != null && !stsDataDetails2.isEmpty()) {
                data.put("yesterdayData", stsDataDetails2.get(0));
            }
        }
        return data;
    }


    /**
     * 统计客流数据列表
     */
    //@RequiresPermissions("sts:detail:list")
    @PostMapping("/holidayStatistic")
    @ResponseBody
    public List<StsHolidayDataDetail> selectStsDataHolidayStatisticList(String deptIds){
        if(deptIds == null || "".equals(deptIds)) return null;
        return stsDataDetailService.selectStsDataDeptStatisticHolidayList(Convert.toStrArray(deptIds));
    }
}
